package com.yu.Demo01;

public class Ticket implements Runnable{
    /**
     * 复习synch 实现加锁
     */
    private int number = 30;
    private int count = 0;
    public synchronized void sale(){
        if (number > 0){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "卖出了" + (++count) + "还剩" + --number);
        }
    }

    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        Thread thread0 = new Thread(ticket);
        Thread thread1 = new Thread(ticket);
        Thread thread2 = new Thread(ticket);
        thread0.start();
        thread1.start();
        thread2.start();
    }

    @Override
    public void run() {
        while (true) {
            sale();
        }
    }
}
